﻿#region

using System;
using EsbMonitorExtension.MonitorTasks;
using EsbMonitorMessages.Domain;
using NServiceBus.Unicast;
using NUnit.Framework;
using Rhino.Mocks;
using Utilities;

#endregion

namespace EsbMonitorExtension.Tests.InitializeEsbMonitorTests
{
    [TestFixture]
    public class when_initializing_esb_monitor : observations_for_sut<InitializeEsbMonitor>
    {

        private IUnicastBus _busMock;
        private IMonitorTasksBuilder _monitorTasksBuilderMock;

        private ServiceInfo _serviceInfoFake;

        private IMonitorTask _serviceStartedTaskMock;
        private IMonitorTask _publisherStartedTaskMock;
        private IMonitorTask _handlerStartedTaskMock;
        private IMonitorTask _messageAssembliesLoadedTaskMock;
        private IMonitorTask _initServiceAliveServiceTaskMock;

        protected override void EstablishContext()
        {
   
            _serviceInfoFake = new ServiceInfo(
                "fakeName",
                "fakeSummary",
                "fakeVersion",
                new InputQueueInfo("fakeInputQueue"),
                new InputQueueInfo("fakeErrorQueue"),
                new InputQueueInfo("fakeAuditQueue")
                );

            _busMock = MockRepository.GenerateMock<IUnicastBus>();

            _monitorTasksBuilderMock = MockRepository.GenerateMock<IMonitorTasksBuilder>();
            _serviceStartedTaskMock = MockRepository.GenerateMock<IMonitorTask>();
            _publisherStartedTaskMock = MockRepository.GenerateMock<IMonitorTask>();
            _handlerStartedTaskMock = MockRepository.GenerateMock<IMonitorTask>();
            _messageAssembliesLoadedTaskMock = MockRepository.GenerateMock<IMonitorTask>();
            _initServiceAliveServiceTaskMock = MockRepository.GenerateMock<IMonitorTask>();

            UnicastBusExtensions.PrepareServiceInfoFunc = x => _serviceInfoFake;

            _monitorTasksBuilderMock.Stub(x => x.BuildServiceStartedTask(_busMock)).Return(_serviceStartedTaskMock);
            _monitorTasksBuilderMock.Stub(x => x.BuildPublisherStartedTask(_busMock)).Return(_publisherStartedTaskMock);
            _monitorTasksBuilderMock.Stub(x => x.BuildHandlerStartedTask(_busMock)).Return(_handlerStartedTaskMock);
            _monitorTasksBuilderMock.Stub(x => x.BuildMessageAssembliesLoadedTask(_busMock)).Return(_messageAssembliesLoadedTaskMock);
            _monitorTasksBuilderMock.Stub(x => x.BuildInitServiceAliveServiceTask(_busMock)).Return(_initServiceAliveServiceTaskMock);

            Sut = new InitializeEsbMonitor();
            Sut.Bus = _busMock;
            Sut.MonitorTasksBuilder = _monitorTasksBuilderMock;
        }
  
        protected override void Because()
        {
            Sut.Run();
        }

        [Test]
        public void should_execute_serviceStartedTask()
        {
            _serviceStartedTaskMock.AssertWasCalled(x => x.RunTask());
        } 
        
        [Test]
        public void should_execute_publisherStartedTask()
        {
            _publisherStartedTaskMock.AssertWasCalled(x => x.RunTask());
        } 
        
        [Test]
        public void should_execute_handlerStartedTask()
        {
            _handlerStartedTaskMock.AssertWasCalled(x => x.RunTask());

        }  
        
        [Test]
        public void should_execute_messageAssembliesLoadedTask()
        {
            _messageAssembliesLoadedTaskMock.AssertWasCalled(x => x.RunTask());

        }  
        
        [Test]
        public void should_execute_initServiceAliveServiceTask()
        {
            _initServiceAliveServiceTaskMock.AssertWasCalled(x => x.RunTask());
        }
    }
}